//
//  WZPMeTableController.m
//  WZPItem
//
//  Created by 王王喆平 on 16/7/3.
//  Copyright © 2016年 UI. All rights reserved.
//

#import "WZPMeTableController.h"
#import "WZPSettingViewController.h"
#import <AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "WZPMeSquare.h"
#import "WZPSquareCollectionCell.h"
#import <SafariServices/SafariServices.h>
#define itemWH ((WZPScreenW - (clos -1)* margin)/clos)
#import "WZPWebViewController.h"

static  NSString * ID = @"cell";
static  NSInteger clos = 5;
static  CGFloat margin = 1;

@interface WZPMeTableController ()<UICollectionViewDataSource,UICollectionViewDelegate,SFSafariViewControllerDelegate>

@property (nonatomic,strong) NSMutableArray * allitem;
@property (nonatomic,weak) UICollectionView * collView;

@end

@implementation WZPMeTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setFootView];
    
    [self setNavigation];
    
    [self laydata];
    
    // 默认group样式tableView,每一组自带组间距
    self.tableView.sectionFooterHeight = 10;
    self.tableView.sectionHeaderHeight = 0;
    //group样式tableView 第0 个cell会有一个默认y值 35
    self.tableView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0);

}
// 请求数据
-(void)laydata{
    AFHTTPSessionManager * manageer = [AFHTTPSessionManager manager];
    
    // 创建数组保存数据
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    dict[@"a"] = @"square";
    dict[@"c"] = @"topic";
    [manageer GET:WZPHtp parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 获取字典数组
        NSArray * array =responseObject[@"square_list"];
        _allitem = [WZPMeSquare mj_objectArrayWithKeyValuesArray:array];
        
        // 计算总行数
        NSInteger nums = (_allitem.count - 1)/clos + 1;
        // 计算collView 的高度
        CGFloat collViewH = nums * itemWH +(nums - 1) * margin;
        _collView.wzp_height = collViewH;
        // 通知tablbView collView 大小
        self.tableView.tableFooterView = _collView;
        
        [self resolveData];
        
        [_collView reloadData];
//        [responseObject writeToFile:@"/Users/wangwangzheping/Desktop/项目/01/01-题目/wzp2.plist" atomically:YES];
       } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}
// 解决数据 不足行数问题
-(void)resolveData{
    NSInteger extre = _allitem.count % clos;
    if (extre) {
        extre = clos - extre;
        for (int i = 0; i < extre; i++) {
            WZPMeSquare * square = [[WZPMeSquare alloc]init];
            [_allitem addObject:square];
        }
    }

}
// 设置底部View 流水布局
-(void)setFootView{
    
    // UICollectionViewFlowLayout 具有布局效果
    UICollectionViewFlowLayout * layout =({
    layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置cell 尺寸
    layout.itemSize = CGSizeMake(itemWH, itemWH);
    layout.minimumInteritemSpacing = margin;
    layout.minimumLineSpacing = margin;
    layout;
    });
    // tableView的顶部 底部View x , y , w 由系统决定, 所以我们只能决定它的高度
    //UICollectionView  backgroundColor 默认是黑色的
    UICollectionView * collView =({
    collView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 300) collectionViewLayout:layout];
    collView.backgroundColor = [UIColor clearColor];
    // 设置数据源
    collView.dataSource = self;
    // 设置调用
    collView.delegate =self;
    //注册cell
    [collView registerNib:[UINib nibWithNibName:@"WZPSquareCollectionCell" bundle:nil] forCellWithReuseIdentifier:ID];
    collView;
    });
    _collView = collView;
    
    self.tableView.tableFooterView = collView;
    
}
#pragma mark UICollectionViewDelegate
// 点击cell 调用
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    // 获取模型
    WZPMeSquare *item = _allitem[indexPath.row];

    if (![item.url containsString:@"http"]) return;
    
    WZPWebViewController * webVc = [[WZPWebViewController alloc]init];
    webVc.url = [NSURL URLWithString:item.url];
    [self.navigationController pushViewController:webVc animated:YES];
    // 只用iOS9可以使用跳转网页
//    SFSafariViewController *safariVc = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:item.url]];
//    // 苹果做了处理
//    [self presentViewController:safariVc animated:YES completion:nil];
}
#pragma mark UICollectionViewDataSource

// 返回多少个cell
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _allitem.count;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WZPSquareCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.item = _allitem[indexPath.row];

    return cell;

}


// 设置标题
-(void)setNavigation{
   self.navigationItem.title = @"我的";

    UIBarButtonItem * setbtn =[UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-setting-icon"]hlightImage:[UIImage imageNamed:@"mine-setting-icon-click"] target:self action:@selector(setbtnView)];

    UIBarButtonItem * nights = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-moon-icon"] setImage:[UIImage imageNamed:@"mine-moon-icon-click"] target:self action:@selector(nightbtn:)];
    
    self.navigationItem.rightBarButtonItems =@[setbtn,nights];
}
// 点击了设置
-(void)setbtnView{

    WZPSettingViewController * settingVc = [[WZPSettingViewController alloc]init];
    // 隐藏顶部tabBar 条
    settingVc.hidesBottomBarWhenPushed = YES;
    //哪个对象调用,就去那个类里面寻找对应的方法
    [self.navigationController pushViewController:settingVc animated:YES];
    

}
// 夜晚模式
-(void)nightbtn:(UIButton *)btn{

    btn.selected = !btn.selected;
}



@end
